Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typeforce

Package Overview
Dependencies
Maintainers
1
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typeforce

Another biased type checking solution for Javascript

  • 1.18.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
186K
decreased by-14.88%
Maintainers
1
Weekly downloads
 
Created

What is typeforce?

Typeforce is a runtime type-checking library for JavaScript. It allows developers to enforce types and structures on their data, ensuring that the data conforms to expected formats. This can be particularly useful for validating function arguments, API responses, and more.

What are typeforce's main functionalities?

Basic Type Checking

Typeforce provides basic type checking for primitive types like strings, numbers, booleans, etc. The example demonstrates how to check if a value is a string.

const typeforce = require('typeforce');

// Define a type check for a string
const isString = typeforce.String;

// Validate a value
console.log(isString('Hello, World!')); // true
console.log(isString(123)); // throws an error

Custom Type Definitions

Typeforce allows you to define custom type checks using functions. The example shows how to create a custom type check for even numbers.

const typeforce = require('typeforce');

// Define a custom type check
const isEvenNumber = typeforce(function(value) {
  return typeof value === 'number' && value % 2 === 0;
});

// Validate a value
console.log(isEvenNumber(4)); // true
console.log(isEvenNumber(5)); // throws an error

Object Structure Validation

Typeforce can validate the structure of objects, ensuring that they have the expected properties with the correct types. The example demonstrates how to validate an object representing a person.

const typeforce = require('typeforce');

// Define a type check for an object structure
const personType = {
  name: typeforce.String,
  age: typeforce.Number
};

// Validate an object
const person = { name: 'Alice', age: 30 };
console.log(typeforce(personType, person)); // true

const invalidPerson = { name: 'Bob', age: 'thirty' };
console.log(typeforce(personType, invalidPerson)); // throws an error

Array Type Checking

Typeforce can validate arrays, ensuring that all elements conform to a specified type. The example shows how to check if an array contains only numbers.

const typeforce = require('typeforce');

// Define a type check for an array of numbers
const isArrayofNumbers = typeforce(typeforce.ArrayOf(typeforce.Number));

// Validate an array
console.log(isArrayofNumbers([1, 2, 3])); // true
console.log(isArrayofNumbers([1, '2', 3])); // throws an error

Other packages similar to typeforce

Keywords

FAQs

Package last updated on 10 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc